#!/bin/sh

alias curl='curl -stderr'
alias echo='echo -e'
#clear

export _eval=`date +'%N'` #randomize file name so more eval can run in parallel

#Yahoo quote
export YahooQuote=`curl "http://download.finance.yahoo.com/d/quotes.csv?s=$1&f=snpol1p2wmryva2j1n"` #last 'n' is dummy to avoid '^M'
export Name=`echo $YahooQuote|cut -d, -f2`
export Previous=`echo $YahooQuote|cut -d, -f3`
export Open=`echo $YahooQuote|cut -d, -f4`
export RealTime=`echo $YahooQuote|cut -d, -f5`
export Change=`echo $YahooQuote|cut -d, -f6 |sed 's/"//g'`
export YearRange=`echo $YahooQuote|cut -d, -f7`
export DayRange=`echo $YahooQuote|cut -d, -f8`
export PE=`echo $YahooQuote|cut -d, -f9|sed 's/N\/A//g'` 
export Dividend=`echo $YahooQuote|cut -d, -f10 | sed 's/N\/A/0.0/g'`
export Volume=`echo $YahooQuote|cut -d, -f11 | sed 's/N\/A//g'`
export AvgVolume=`echo $YahooQuote|cut -d, -f12 | sed 's/N\/A//g'`
export VolDiff=`echo "scale=2; $Volume / $AvgVolume"|bc` 
export Capital=`echo $YahooQuote|cut -d, -f13 | sed 's/N\/A//g'`

#Earning date
export page=`echo $1 |cut -c 1`
export EarningDate=`curl http://biz.yahoo.com/research/earncal/$page/$1.html |egrep "[0-9], 200?" |cut -d"<" -f1 |tr "," " "`
export earningdate=`date +%m/%d/%Y -d "$EarningDate"`
#Yahoo's Analyst's rank
export AnalystThisWeek=`curl "http://finance.yahoo.com/q/ao?s=$1" |grep -i "Mean Recommendation" |grep -o "[1-5]\.[0-9]" |head -n 1`
export AnalystLastWeek=`curl "http://finance.yahoo.com/q/ao?s=$1" |grep -i "Mean Recommendation" |grep -o "[1-5]\.[0-9]" |head -n 2 |tail -n 1`

#Up/Downgrade recently
export updown=`grep -H -w $1 *grade|cut -d: -f1` #List grabbed in fetch

#insdustry and trend
export industryquery=`curl "http://finance.yahoo.com/q/in?s=$1" |sed 's/\<tr\>/\n/g' |egrep -B 1 -A 1 "\^YHO[a-zA-Z][0-9]{1,4}"`
export industry=`echo $industryquery |egrep -o ">[A-Z][a-z].*[A-Za-z]<"|sed 's/<//g' |sed 's/>//g' |sed 's/,//g'`
export industryupdown=`echo $industryquery |egrep  -o "Down|Up" |sed 's/Down/-/g' |sed 's/Up/+/g'`
export industrypercent=`echo $industryquery |egrep -o ">[0-9]+.[0-9]+%" |sed 's/>//g'`

#when is last time upgrade/downgrade
export lastimeupdown=`curl "http://finance.yahoo.com/q/ud?s=$1" |sed 's/tr/\n/g'|egrep  "[0-9]{1,2}-[A-Z][a-z]+" |head -n 1 |egrep -o "[0-9]{1,2}-[A-Z][a-z]+-[0-9]{1,2}|Upgrade|Downgrade|Initiated"`
export lastupdowndate=`echo $lastimeupdown|awk '{print $1}'`
export dateinnum=`echo $lastupdowndate |sed -e 's/Jan/0/g' -e 's/Feb/1/g' -e 's/Mar/2/g' -e 's/Apr/3/g' -e 's/May/4/g' -e 's/Jun/5/g' -e 's/Jul/6/g' -e 's/Aug/7/g' -e 's/Sep/8/g' -e 's/Oct/9/g' -e 's/Nov/10/g' -e 's/Dev/11/g'`
simpleyear=`echo $dateinnum |cut -d- -f3`
year=`echo $simpleyear + 2000 |bc`
month=`echo $dateinnum |cut -d- -f2`
day=`echo $dateinnum |cut -d- -f1`   
priceat=`curl -stderr "http://table.finance.yahoo.com/d?a=$month&b=$day&c=$year&d=$month&e=$day&f=$year&g=d&s=$1" |sed 's/tr/\n/g' |egrep  "[0-9]+\.[0-9]+|[0-9]+\,[0-9]+" |cut -d'<' -f25- |sed 's/</ /g'  |sed 's/>/ /g' |awk '{print $8}'`

echo "----------------------------------------------------------------------"
echo Name,Industry|awk -F ',' '{printf "%-22s|%-45s|\n",$1,$2}'
echo $Name,$industry|awk -F ',' '{printf "%-22s|%-45s|\n",$1,$2}'
echo "----------------------------------------------------------------------"
echo Quote,Volume,Change,Sector,Cap,Div%,Earning,Thiswk,Lastwk\
|awk -F ',' '{printf "%-7s|%-6s|%-7s|%-6s|%-7s|%-5s|%-10s|%-6s|%-6s|\n",$1,$2,$3,$4,$5,$6,$7,$8,$9}'
echo "----------------------------------------------------------------------"
echo $RealTime,$VolDiff,$Change,$industryupdown,$industrypercent,$Capital,$Dividend,$earningdate,$AnalystThisWeek,$AnalystLastWeek\
|awk -F ',' '{printf "$%6.2f|%-6s|%-7s|%c%5s|%-7s|%-5s|%-10s|%-6s|%-6s|\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}'
echo "----------------------------------------------------------------------"

#Fundemental Analysis----------------------------------------------"
./fa.sh $1
#Intricnsic Value
./investinvalue.sh $1

echo "Today's Radar Screen---------------------------------------"
find . -maxdepth 1 -name "*List" |grep -v CovestList|xargs grep -w $1 |sort -r
find ./intraday |xargs grep $1

echo "Recent Radar Screen----------------------------------------"
find ./data* -name "*List"  -ctime -5 |grep -v EarningList| xargs grep -w $1 |sort -r
find ./data* -name "qry*"  -ctime -5  | xargs grep -w $1 |sort -r
find ./data* -name "stockcharts*"  -ctime -5 |xargs grep -w $1 |sort -r

echo "TA patterns------------------------------------------------"
grep -w $1 stockchart.dat  |awk '{print $2}'

echo "TA Statistics----------------------------------------------"
curl "http://download.finance.yahoo.com/d/quotes.csv?s=$1&f=m8m6" |awk 'BEGIN { FS = "," } ;{printf("50 MA=%2.2f%\t200MA=%2.2f%\n",$1,$2)}'
#echo "Fibonacci Analysis (30 days-Short)"
#Term, 38.1% retracement, 50% retracement, 61.8% retracement
curl -stderr "http://www.stockta.com/cgi-bin/analysis.pl?symb=$1&table=fib&mode=table" |egrep -A 4 "<td>Short</td>"| sed -e 's/<td><font color="#ff0000">//g' -e 's/<td><font color="#008000">//g' -e 's/<td><font color="#000000">//g' -e 's/<\/font><\/td>//g' -e's/&nbsp;//g' -e 's/<td>//g' -e 's/<\/td>//g' |sed 's/^.$/null/g' |tr '\n' ',' |awk '{printf("Fibonacci=%s\n",$0)}'
#echo "RSI Analysis (<30 Oversold, >70 Ovwerbought)"
curl -stderr "http://www.stockta.com/cgi-bin/analysis.pl?symb=$1&table=rsi&mode=table" |grep "RSI Analysis" |egrep -o ">[0-9.]+<" |sed -e 's/<//g' -e 's/>//g' |awk '{printf("RSI=%f(<30 Oversold, >70 Overbought)\n"),$1}'

echo "History Prices---------------------------------------------"
./history.sh $1 10

echo "Recent News------------------------------------------------"
curl "http://finance.yahoo.com/rss/headline?s=$1" |sed 's/>/\n/g' |egrep  "/pubDate|/title" |sed 's/pubDate//g' |sed 's/title//g' |head -n 8 |tail -n 6|tr '\n' ' '|sed 's/<\//------/g' |sed 's/GMT/@/g' |tr '@' '\n'

echo "Sentiments--------------------------------------------------"
./yahoomsgboard.sh $1

echo "Pivot Point at diff resist/support level -------------------"
curl "http://pitrading.com/pivot_points.php?symbol=$1&time_frame=Daily"  | egrep -o 'value="[0-9]+.[0-9]+"' | cut -d'"' -f2 |tr '\n' ' ' |awk '{printf("R1=%3.2f,R2=%3.2f,R1=%3.2f,Pivot=%3.2f,S1=%3.2f,S2=%3.2f,S3=%3.2f\n",$1,$2,$3,$4,$5,$6,$7)}'


\rm -f $_eval tmp$$ tmplong$$ tmpint$$ tmpshort$$

